package cn.com.ddapp.ddm.fragment.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.ClassFicationLandSortAdpter;
import cn.com.ddapp.ddm.adapter.ClassFicationRootAdpter;
import cn.com.ddapp.ddm.adapter.ClassFicationSortAdpter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.GoodSmallSortBean;
import cn.com.ddapp.ddm.bean.GoodSortBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.GoodClassifyActivity;
import cn.com.ddapp.ddm.ui.GoodLandClassifyActivity;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableGridView;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/8/16 0016.
 */
public class ClassificationFragment extends BaseFragment {

    @BindView(R.id.class_rootlist)
    ListView class_rootlist;
    @BindView(R.id.class_rootName)
    TextView class_rootName;
    @BindView(R.id.class_PullView)
    PullableGridView class_PullView;
    @BindView(R.id.class_pullLayout)
    PullToRefreshLayout class_pullLayout;
    @BindView(R.id.empty)
    EmptyLayout empty;
    @BindView(R.id.img_error_layout)
    ImageView img_error_layout;
    @BindView(R.id.class_bannerlist)
    ImageView class_bannerlist;
    private List<GoodSortBean.GoodSort> list;
    private List<GoodSmallSortBean.GoodSmallSort> slist;
    private ClassFicationRootAdpter rAdapter;
    private ClassFicationSortAdpter sAdapter;
    private ClassFicationLandSortAdpter slandAdapter;
    private String TAG="ClassificationFragment";
    private boolean isF=true;
    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
//        slist = new ArrayList<>();
//            for (int b = 'a'; b <= 'z'; b++) {
//                slist.add("" + (char) b);
//            }

        if(TDevice.isTablet(getActivity())){
            initLand();
            requestSLanddata(1,true);
//            int width=(int)(TDevice.getScreenWidth()-400);
//            int height=(int)(TDevice.getScreenWidth()-400)/4;
//            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width,height);
//            class_bannerlist.setLayoutParams(params);
            class_bannerlist.setVisibility(View.GONE);
        }
        else{
            initPro();
            requestSdata(1,true);
//            int width=(int)(TDevice.getScreenWidth()-350);
//            int height=(int)(TDevice.getScreenWidth()-350)/3;
//            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width,height);
//            class_bannerlist.setLayoutParams(params);
        }

    }

    private void initPro() {
        requestData(true);

        class_pullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                class_pullLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                class_pullLayout.loadmoreFinish(PullToRefreshLayout.NODATA);
            }
        });
    }

    private void initLand() {
        requestLandData(true);

        class_pullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                class_pullLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                class_pullLayout.loadmoreFinish(PullToRefreshLayout.NODATA);
            }
        });
    }

    private void requestLandData(final boolean isFirst) {
        empty.setErrorType(EmptyLayout.NETWORK_LOADING);
        int parentId=0;
        final OkUtilCallback okCallback=new OkUtilCallback(empty) {
            @Override
            public void onMyResponse(String response, int id) {
//                Log.i(TAG,response);
                GoodSortBean goodSort=gson.fromJson(response,GoodSortBean.class);
                if((!AppConfig.RESULT_OK.equalsIgnoreCase(goodSort.getResult()))){
                    empty.setErrorType(EmptyLayout.NODATA);
                }
                else if((!AppConfig.RESULT_OK.equalsIgnoreCase(goodSort.getResult()))){
                    BaseApplication.getIntstance().showToast(goodSort.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                }else if((isFirst)&&goodSort.getSortList().size()==0){
                    empty.setErrorType(EmptyLayout.NODATA);
                }

                else{
                    list = goodSort.getSortList();
                    rAdapter = new ClassFicationRootAdpter(getActivity(), list);
                    class_rootlist.setAdapter(rAdapter);
//                        requestSdata(list.get(0).getId(),true);
                    class_rootName.setText(list.get(0).getName());
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        public void run() {
                            TextView tt= (TextView)  class_rootlist.getChildAt(0).findViewById(R.id.cf_root_name);
                            tt.setTextColor(Color.RED);
                        }
                    }, 500);
                    class_rootlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            for(int a=0;a<list.size();a++){
                                TextView tt= (TextView) class_rootlist.getChildAt(a).findViewById(R.id.cf_root_name);
                                tt.setTextColor(0xaf000000);
                            }
                            TextView tt= (TextView) class_rootlist.getChildAt(i).findViewById(R.id.cf_root_name);
                            tt.setTextColor(Color.RED);
                            class_rootName.setText(list.get(i).getName());
                            int smallId=list.get(i).getId();
                            String lId=list.get(i).getLevelId();
                            SPUtils.setStringData(getActivity(),SPUtils.LID,lId);
                            requestSLanddata(smallId,isF);

                        }
                    });
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                empty.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        OKUtilAPI.post(OKUtilAPI.goodsort,"parentId=" + parentId,okCallback);
    }

    private void requestData(final boolean isFirst) {
            empty.setErrorType(EmptyLayout.NETWORK_LOADING);
            int parentId=0;
            final OkUtilCallback okCallback=new OkUtilCallback(empty) {
                @Override
                public void onMyResponse(String response, int id) {
//                    Log.i(TAG,response);
                    GoodSortBean goodSort=gson.fromJson(response,GoodSortBean.class);
                    if((!AppConfig.RESULT_OK.equalsIgnoreCase(goodSort.getResult()))){
                        empty.setErrorType(EmptyLayout.NODATA);
                    }
                    else if((!AppConfig.RESULT_OK.equalsIgnoreCase(goodSort.getResult()))){
                        BaseApplication.getIntstance().showToast(goodSort.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                    }else if((isFirst)&&goodSort.getSortList().size()==0){
                        empty.setErrorType(EmptyLayout.NODATA);
                    }

                    else{
                        list = goodSort.getSortList();
                        rAdapter = new ClassFicationRootAdpter(getActivity(), list);
                        class_rootlist.setAdapter(rAdapter);
//                        requestSdata(list.get(0).getId(),true);
                        class_rootName.setText(list.get(0).getName());
                        Handler handler = new Handler();
                                 handler.postDelayed(new Runnable() {
                                         public void run() {
                                             TextView tt= (TextView)  class_rootlist.getChildAt(0).findViewById(R.id.cf_root_name);
                                             tt.setTextColor(Color.RED);
                                         }
                                    }, 500);
                        class_rootlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                 for(int a=0;a<list.size();a++){
                                     if(class_rootlist.getChildAt(a)!=null) {
                                         TextView tt = (TextView) class_rootlist.getChildAt(a).findViewById(R.id.cf_root_name);
                                         tt.setTextColor(0xaf000000);
                                         RelativeLayout rl = (RelativeLayout) class_rootlist.getChildAt(a).findViewById(R.id.sort_rl);
                                         rl.setBackgroundColor(getResources().getColor(R.color.white));
                                     }
                                 }
                                TextView tt= (TextView) class_rootlist.getChildAt(i).findViewById(R.id.cf_root_name);
                                tt.setTextColor(Color.RED);
                                RelativeLayout rl= (RelativeLayout) class_rootlist.getChildAt(i).findViewById(R.id.sort_rl);
                                rl.setBackgroundColor(getResources().getColor(R.color.bgcolor));
                                class_rootName.setText(list.get(i).getName());
                                int smallId=list.get(i).getId();
                                String lId=list.get(i).getLevelId();
                                SPUtils.setStringData(getActivity(),SPUtils.LID,lId);
                                    requestSdata(smallId,isF);

                            }
                        });
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    empty.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
            };
        OKUtilAPI.post(OKUtilAPI.goodsort,"parentId=" + parentId,okCallback);
    }

    private void requestSLanddata(int smallId,final boolean isFirst) {
        empty.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okCallback=new OkUtilCallback(empty) {
            @Override
            public void onMyResponse(String response, int id) {
                GoodSmallSortBean bean=gson.fromJson(response,GoodSmallSortBean.class);
                if((!AppConfig.RESULT_OK.equalsIgnoreCase(bean.getResult()))){
                    empty.setErrorType(EmptyLayout.NODATA);
                }
                else if((!AppConfig.RESULT_OK.equalsIgnoreCase(bean.getResult()))){
                    BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                }else if((isFirst)&&bean.getSortList().size()==0){
                    empty.setErrorType(EmptyLayout.NODATA);
                }else {
                    slist = bean.getSortList();
                    slandAdapter=new ClassFicationLandSortAdpter(getActivity(),slist);
                    class_PullView.setNumColumns(7);
                    class_PullView.setAdapter(slandAdapter);
                    class_PullView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Intent intent = new Intent(getActivity(), GoodLandClassifyActivity.class);
                            intent.putExtra("classiftId",slist.get(i).getLevelId());
                            intent.putExtra("sortName",slist.get(i).getName());
                            String lId=slist.get(i).getLevelId();

                            startActivity(intent);
                        }
                    });

                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                empty.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        OKUtilAPI.post(OKUtilAPI.getGoodsort,"parentId=" + smallId,okCallback);
    }

    private void requestSdata(int smallId,final boolean isFirst) {
        empty.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okCallback=new OkUtilCallback(empty) {
            @Override
            public void onMyResponse(String response, int id) {
                GoodSmallSortBean bean=gson.fromJson(response,GoodSmallSortBean.class);
                if((!AppConfig.RESULT_OK.equalsIgnoreCase(bean.getResult()))){
                    empty.setErrorType(EmptyLayout.NODATA);
                }
                else if((!AppConfig.RESULT_OK.equalsIgnoreCase(bean.getResult()))){
                    BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                }else if((isFirst)&&bean.getSortList().size()==0){
                    empty.setErrorType(EmptyLayout.NODATA);
                }else {
                    slist = bean.getSortList();
                    sAdapter=new ClassFicationSortAdpter(getActivity(),slist);
                    class_PullView.setAdapter(sAdapter);
                    class_PullView.setNumColumns(3);
                    class_PullView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Intent intent = new Intent(getActivity(), GoodClassifyActivity.class);
                            intent.putExtra("classiftId",slist.get(i).getLevelId());
                            intent.putExtra("claName",slist.get(i).getName());
                            String lId=slist.get(i).getLevelId();
                            startActivity(intent);
                        }
                    });

                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                empty.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        OKUtilAPI.post(OKUtilAPI.getGoodsort,"parentId=" + smallId,okCallback);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.classfication_fragment;
    }

    @OnClick({R.id.img_error_layout})
    public void onClick(View view){
    switch (view.getId()){
        case R.id.img_error_layout:
            if(TDevice.isTablet(getActivity())){
                requestLandData(true);
            }
            else{
                requestData(true);
            }
            break;
    }
    }
}
